//
//  JFGuidePagesViewController.m
//  JFApp
//
//  Created by Alan Wang on 2019/3/20.
//  Copyright © 2019 Alan Wang. All rights reserved.
//

#import "JFGuidePagesViewController.h"

@interface JFGuidePagesViewController ()<UIScrollViewDelegate>
//滚动容器
@property (nonatomic,strong)UIScrollView *imageScrollView;
//分页按钮
@property (nonatomic,strong) UIPageControl *pageControl;

//下一步按钮
@property (nonatomic,strong)UIButton *nextBtn;


@end

@implementation JFGuidePagesViewController

-(instancetype)init{
    self = [super init];
    if(self){
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatUI];
}


-(void)creatUI{
    
    //初始化滑动视图
    _imageScrollView = [[UIScrollView alloc]initWithFrame:self.view.bounds];
    _imageScrollView.delegate = self;
    _imageScrollView.bounces = YES;
    _imageScrollView.pagingEnabled = YES;
    _imageScrollView.showsVerticalScrollIndicator = NO;
    _imageScrollView.showsHorizontalScrollIndicator = NO;
    _imageScrollView.contentSize = CGSizeMake(SCREEN_WIDTH * 3, 0);
    
    if (@available(iOS 13.0, *)) {
        _imageScrollView.automaticallyAdjustsScrollIndicatorInsets = NO;
    } else {
        // Fallback on earlier versions
    }
    [self.view addSubview:_imageScrollView];
    for (int i = 0; i < 3; i++) {
        UIImageView *imageView = [[UIImageView alloc]init];
        imageView.userInteractionEnabled = YES;
        imageView.frame = CGRectMake(SCREEN_WIDTH * i, 0, SCREEN_WIDTH, _imageScrollView.height);
        [imageView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"mg%d", i + 1]]];
        [_imageScrollView addSubview:imageView];
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH * i, _imageScrollView.height - 160, SCREEN_WIDTH, 100)];
        label.textColor = UIColorFromHex(0x708A6A);
        label.numberOfLines = 0;
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont fontWithName:@"PingFang SC" size: 20];
        NSMutableAttributedString *attStr;
        switch (i) {
            case 0:
                {
                    attStr = [[NSMutableAttributedString alloc] initWithString:@"Safeguard wide\n range of assets under our\n global custodial network" attributes:@{NSFontAttributeName: [UIFont fontWithName:@"PingFang SC" size: 20],NSForegroundColorAttributeName: [UIColor colorWithRed:112/255.0 green:138/255.0 blue:106/255.0 alpha:1.0]}];
                    [attStr addAttributes:@{NSFontAttributeName: [UIFont fontWithName:@"PingFang SC" size: 24], NSForegroundColorAttributeName: [UIColor colorWithRed:248/255.0 green:168/255.0 blue:40/255.0 alpha:1.0]} range:[attStr.string rangeOfString:@"global custodial network"]];
                }
                break;
            case 1:
            {
                attStr = [[NSMutableAttributedString alloc] initWithString:@"Backed\n by licensed Trust company,\nprotected by bank-grade security" attributes:@{NSFontAttributeName: [UIFont fontWithName:@"PingFang SC" size: 20],NSForegroundColorAttributeName: [UIColor colorWithRed:112/255.0 green:138/255.0 blue:106/255.0 alpha:1.0]}];
                [attStr addAttributes:@{NSFontAttributeName: [UIFont fontWithName:@"PingFang SC" size: 24], NSForegroundColorAttributeName: [UIColor colorWithRed:248/255.0 green:168/255.0 blue:40/255.0 alpha:1.0]} range:[attStr.string rangeOfString:@"Backed"]];
                [attStr addAttributes:@{NSFontAttributeName: [UIFont fontWithName:@"PingFang SC" size: 24], NSForegroundColorAttributeName: [UIColor colorWithRed:248/255.0 green:168/255.0 blue:40/255.0 alpha:1.0]} range:[attStr.string rangeOfString:@"protected"]];
            }
                break;
            case 2:
            {
                attStr = [[NSMutableAttributedString alloc] initWithString:@"Deposit, transfer,\n exchange, withdraw and pay \nanytime anywhere" attributes:@{NSFontAttributeName: [UIFont fontWithName:@"PingFang SC" size: 20],NSForegroundColorAttributeName: [UIColor colorWithRed:112/255.0 green:138/255.0 blue:106/255.0 alpha:1.0]}];
                [attStr addAttributes:@{NSFontAttributeName: [UIFont fontWithName:@"PingFang SC" size: 24], NSForegroundColorAttributeName: [UIColor colorWithRed:248/255.0 green:168/255.0 blue:40/255.0 alpha:1.0]} range:[attStr.string rangeOfString:@"anytime anywhere"]];
            }
                break;
            default:
                break;
        }
        label.attributedText = attStr;
        [_imageScrollView addSubview:label];

        if (i == 2) {
            UIButton *sender = [UIButton buttonWithType:UIButtonTypeCustom];
            sender.frame = CGRectMake(SCREEN_WIDTH * 2 + (SCREEN_WIDTH - 100) / 2, _imageScrollView.height - 50, 100, 30);
            [sender setTitle:@"Try Now" forState:UIControlStateNormal];
            sender.layer.cornerRadius = 15;
            sender.layer.borderWidth = 1;
            sender.layer.borderColor = UIColorFromHex(0x708A6A).CGColor;
            [sender setTitleColor:UIColorFromHex(0x708A6A) forState:UIControlStateNormal];
            sender.titleLabel.font = [UIFont fontWithName:@"PingFang SC" size: 15];
            [sender addTarget:self action:@selector(tryAction) forControlEvents:UIControlEventTouchUpInside];
            [_imageScrollView addSubview:sender];
        }
        
    };
    
    
    //初始化分页视图
    _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, _imageScrollView.height - 60, SCREEN_WIDTH, 20)];
    _pageControl.pageIndicatorTintColor = UIColorFromHex(0xCCCCCC);
    _pageControl.currentPageIndicatorTintColor = UIColorFromHex(0x708A6A);
    _pageControl.numberOfPages = 3;
    [self.view addSubview:_pageControl];
}

- (void)tryAction {
    if (self.completeBlock) {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:k_JFApp_FirstLaunch];
        [[NSUserDefaults standardUserDefaults]synchronize];
        self.completeBlock();
    }
}

//滑动视图的代理
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    _imageScrollView.contentOffset = CGPointMake(self.imageScrollView.contentOffset.x, 0);
    CGFloat offsetWidth = scrollView.contentOffset.x;
    int pageNum = offsetWidth / [[UIScreen mainScreen] bounds].size.width;
    self.pageControl.currentPage = pageNum;
    self.pageControl.hidden = (offsetWidth > SCREEN_WIDTH);
}


+(BOOL)isShowGuid{
    if (![[NSUserDefaults standardUserDefaults] boolForKey:k_JFApp_FirstLaunch]) {
        return YES;
    }else{//不是第一次安装
        return NO;
    }
}


@end
